/**
 * Copyright (C) 2010 Neofonie GmbH
 *
 * This programm is free software; you can redistribute it and/or modify
 * it under the terms of the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package eu.dicodeproject.twitterstream;

import org.apache.commons.cli.*;

import java.util.Map;

/**
 * Parse command line parameters given to the Twitter stream tool.
 */
public class ParameterParser {
  /**
   * Parse command line arguments and store the results for future use.
   * @param args the command line arguments to parse.
   * @return the java bean holding all command line arguments.
   * */
  @SuppressWarnings("static-access")
  public Parameters parse(String[] args) {
    Parameters parameters = new Parameters();
    Option usernameOption = OptionBuilder.withArgName("username")
                                         .hasArg()
                                         .withDescription("Username to log into source.")
                                         .create("username");

    Option passwordOption = OptionBuilder.withArgName("password")
                                         .hasArg()
                                         .withDescription("Password to log into source.")
                                         .create("password");

    Option tableOption = OptionBuilder.withArgName("tablename")
                                      .hasArg()
                                      .withDescription("Name of the HBase table to store tweets to")
                                      .create("tablename");

    Option helpOption = OptionBuilder.withDescription("Print usage message and exit.").create("help");

    Options options = new Options();
    options.addOption(usernameOption);
    options.addOption(passwordOption);
    options.addOption(tableOption);
    options.addOption(helpOption);

    CommandLineParser parser = new GnuParser();
    try {
      CommandLine line = parser.parse(options, args);
      if (line.hasOption("help")) {
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp("command", options);
        System.exit(1);
      }
      if (line.hasOption("username")) {
        parameters.setUsername(line.getOptionValue("username"));
      }
      if (line.hasOption("password")) {
        parameters.setPassword(line.getOptionValue("password"));
      }
      if (line.hasOption("tablename")) {
        parameters.setTablename(line.getOptionValue("tablename"));
      }
    } catch (ParseException e) {
      System.err.println("Parsing failed. Details: " + e.getMessage());
      HelpFormatter formatter = new HelpFormatter();
      formatter.printHelp("command", options);
      System.exit(-1);
    }
    return parameters;
  }
}
